package imageutil

import (
	"bytes"
	"encoding/base64"
	"github.com/disintegration/imaging"
	"io/ioutil"
	"os"
)

func Thumbnail(source string, target string) {
	data, _ := os.ReadFile(source)
	buf := bytes.NewBuffer(data)
	image, _ := imaging.Decode(buf)
	newImage := imaging.Thumbnail(image, 150, 150, imaging.Lanczos)
	_ = imaging.Save(newImage, target)
}

func ImagesToBase64(str_images string) []byte {
	//读原图片
	ff, _ := os.Open(str_images)
	defer ff.Close()
	sourcebuffer := make([]byte, 500000)
	n, _ := ff.Read(sourcebuffer)
	//base64压缩
	sourcestring := base64.StdEncoding.EncodeToString(sourcebuffer[:n])
	return []byte(sourcestring)
}

func Base64ToImage(sourcestring []byte, target string) {
	// 写入临时文件
	ioutil.WriteFile("a.png.txt", sourcestring, 0667)
	// 读取临时文件
	cc, _ := ioutil.ReadFile("a.png.txt")

	// 解压
	dist, _ := base64.StdEncoding.DecodeString(string(cc))
	// 写入新文件
	f, _ := os.OpenFile(target, os.O_RDWR|os.O_CREATE, os.ModePerm)
	defer f.Close()
	f.Write(dist)
	return
}
